﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleCompiler
{
    public class StatementFor : Statement
    {
        public Statement for_begin_statement = new Statement();
        public Statement for_step_statement = new Statement();
        public Statement for_condition_expression = new Statement();
        public Statement for_body_statement = new Statement();

        public override Variable Execute(RuntimeVariableScope scope)
        {
            scope.PushScope(false);
            for_begin_statement.Execute(scope);
            Variable bodyVariable = Variable.None;
            while (true)
            {
                TypeBool condition = for_condition_expression.Execute(scope).value as TypeBool;
                if (condition.value)
                {
                    bodyVariable = for_body_statement.Execute(scope);
                    if ((bodyVariable.flag == ControlFlowFlag.For_Break) || (bodyVariable.flag == ControlFlowFlag.Return))
                    {
                        break;
                    }                    
                    for_step_statement.Execute(scope);
                }
                else
                {
                    break;
                }
            }
            scope.PopScope();
            return bodyVariable;
        }

        
    }
}
